package com.gitee.zhanyao.single.system.controller;

import com.gitee.zhanyao.single.system.entity.SysUser;
import com.gitee.zhanyao.single.system.service.SysUserService;
import com.gitee.zhanyao.single.util.datatables.DataTablesRequest;
import com.gitee.zhanyao.single.util.datatables.DataTablesResponse;
import com.gitee.zhanyao.single.util.datatables.Datatables;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/users")
    public DataTablesResponse<SysUser> users(@RequestBody DataTablesRequest<SysUser> dataTablesRequest) {
        Page<SysUser> sysUserPage = sysUserService.getUsers(dataTablesRequest);
        Datatables<SysUser> datatables = new Datatables<>();
        DataTablesResponse<SysUser> dataTablesResponse = datatables.getDataTablesResponse(sysUserPage.getContent(),
                dataTablesRequest.getLength(), sysUserPage.getTotalElements(), dataTablesRequest.getDraw());
        return dataTablesResponse;
    }

}
